package com.fan.txl.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alipay.sdk.app.PayTask;
import com.base.txl.utils.RxUtils;
import com.elvishew.xlog.XLog;
import com.fan.txl.R;
import com.fan.txl.data.base.Rsp;
import com.fan.txl.data.pay.WxPayOrder;
import com.fan.txl.mock.api.service.WxPayService;
import com.fan.txl.ui.base.BaseActivity;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.observers.DisposableObserver;


public class PayUI extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //接口数据 appid
        //getPayInterface().wepay(getWxApi(),"");

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                wepay();
                //alipay();
            }
        });
    }


    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_CHECK_FLAG = 2;
    String payInfo="";
    public void alipay(){


        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                PayTask alipay = new PayTask(PayUI.this);
                String result = alipay.pay(payInfo);
                e.onNext(result);
            }
        })
                .compose(RxUtils.<String>applySchedulers(RxUtils.IO_ON_UI_TRANSFORMER))
                .subscribe(new DisposableObserver<String>() {
                    @Override
                    public void onNext(String s) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public void wepay(){
        getAPIService()
                .getRpcService()
                .rpc(WxPayService.methodPay,WxPayService.service,WxPayService.requestPay)
                .compose(RxUtils.<String>applySchedulers(RxUtils.IO_ON_UI_TRANSFORMER))
                .compose(this.<String>bindUntilEvent(ActivityEvent.STOP))
                .filter(new Predicate<String>() {
                    @Override
                    public boolean test(String wxPayOrderRsp) throws Exception {
                        //return wxPayOrderRsp.getData()!=null;
                        return true;
                    }
                })
                .subscribe(new DisposableObserver<String>() {
                    @Override
                    public void onNext(String wxPayOrderRsp) {
                        XLog.d("aaaaaaa");
                        //getPayInterface().wxpay(getWxApi(),wxPayOrderRsp.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        XLog.d("aaaaaaa");
                    }
                })
                ;

    }

}
